import bean.Student;
import bean.Student02;
import bean.ViewStudent;
import dao.StudentDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 飞哥
 * @version V1.0
 * @package PACKAGE_NAME
 * @date 2021/7/14 8:44
 */
public class MyBatisTest02 {
    @Test
    public void selectStudentByNameOrAgeUse$OrderBy() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //这里的参数email代表按照email的降序排序
        List<Student> list = studentDao.selectStudentByNameOrAgeUse$OrderBy("孙一宁", 20, "email");
        for (Student student : list) {
            System.out.println("学生的信息:" + student);
        }
        sqlSession.close();
    }

    @Test
    public void selectStudentReturnViewStudent() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        ViewStudent viewStudent = dao.selectStudentReturnViewStudent(1001);
        System.out.println(viewStudent);
    }

    @Test
    public void selectCountStudent() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        int i = dao.countStudent();
        System.out.println("学生的总数是：" + i);
    }

    @Test
    public void selectMapById() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Map<Object, Object> map = dao.selectMapById(1001);
        Set<Map.Entry<Object, Object>> entries = map.entrySet();//把map集合转换为set集合然后进行遍历
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println("学生信息：" + entry.getValue());
        }
    }

    @Test
    public void selectAllStudents() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = dao.selectAllStudents();
        for (Student student : list) {
            System.out.println("每个亲爱的学生：" + student);
        }
    }

    @Test
    public void selectAllStudents02() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student02> list = dao.selectAllStudents02();
        for (Student02 student : list) {
            System.out.println("每个亲爱的学生：" + student);
        }
    }

    @Test
    public void selectAllStudents03() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student02> list = dao.selectAllStudents03();
        for (Student02 student : list) {
            System.out.println("每个亲爱的学生：" + student);
        }
    }

    @Test
    public void selectLikeOne() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        String name = "%孙%";
        List<Student> list = dao.selectLikeOne(name);
        for (Student student : list) {
            System.out.println("##" + student);
        }
    }

    @Test
    public void selectLikeTwo() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        String name = "孙";
        List<Student> list = dao.selectLikeTwo(name);
        for (Student student : list) {
            System.out.println("##" + student);
        }
    }
}
